package cn.bluemobi.platform.service.impl;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.bluemobi.common.core.dto.MapDto;
import cn.bluemobi.common.core.dto.Page;
import cn.bluemobi.common.core.dto.PageCondition;
import cn.bluemobi.platform.core.mybatis.PageUtils;
import cn.bluemobi.platform.entity.system.User;
import cn.bluemobi.platform.entity.withdrawals.Withdrawals;
import cn.bluemobi.platform.mapper.WithdrawalsMapper;
import cn.bluemobi.platform.service.WithdrawalsService;

/**
 * 
 * @author lvxh
 * 
 */
@Service
@Transactional
public class WithdrawalsServiceImpl implements WithdrawalsService {

	@Autowired
	private WithdrawalsMapper withdrawalsMapper;

	@Override
	public Page<Withdrawals> findWithdrawalss(PageCondition cond) {
		Page<Withdrawals> page = PageUtils.startPage(cond);
		page.setList(withdrawalsMapper.findWithdrawalss(cond.getMap()));
		return page;
	}

	@Override
	public MapDto saveOrUpdate(Withdrawals withdrawals, User user) {
		MapDto dto = new MapDto();
		if (StringUtils.isEmpty(withdrawals.getWithdrawalsId())) {
			// 新增单页
			withdrawalsMapper.insertWithdrawals(withdrawals);
		} else {
			// 编辑单页
			withdrawalsMapper.updateWithdrawals(withdrawals);
		}
		return dto;
	}

	@Override
	public Withdrawals findWithdrawalsDetail(String withdrawalsId) {
		return withdrawalsMapper.findWithdrawalsDetail(withdrawalsId);
	}

	@Override
	public MapDto deleteWithdrawals(String withdrawalsId, User user) {
		MapDto dto = new MapDto();
		withdrawalsMapper.deleteWithdrawals(withdrawalsId);
		return dto;
	}

	@Override
	public MapDto audit(String withdrawalsId, User user) {
		MapDto dto = new MapDto();
		withdrawalsMapper.audit(withdrawalsId);
		return dto;
	}

	public static boolean htmlFile(String title, String content,
			String filePath, String htmlFile) {
		String str = "";
		long beginDate = (new Date()).getTime();
		try {
			String tempStr = "";
			FileInputStream is = new FileInputStream(filePath);// 读取模块文件
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			while ((tempStr = br.readLine()) != null)
				str = str + tempStr;
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		try {

			str = str.replaceAll("###title###", title);
			str = str.replaceAll("###content###", content);

			File f = new File(htmlFile);
			BufferedWriter o = new BufferedWriter(new FileWriter(f));
			o.write(str);
			o.close();
			System.out.println("共用时：" + ((new Date()).getTime() - beginDate)
					+ "ms");
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
}
